home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / ShareMailGiftware / Frogger / plugins_src / p_ac3 / bitstream.h < prev    next >
C/C++ Source or Header  |  2002-10-28  |  3KB  |  119 lines

  1. /*
  2.  * bitstream.h
  3.  * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org>
  4.  * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
  5.  *
  6.  * This file is part of a52dec, a free ATSC A-52 stream decoder.
  7.  * See http://liba52.sourceforge.net/ for updates.
  8.  *
  9.  * a52dec is free software; you can redistribute it and/or modify
  10.  * it under the terms of the GNU General Public License as published by
  11.  * the Free Software Foundation; either version 2 of the License, or
  12.  * (at your option) any later version.
  13.  *
  14.  * a52dec is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  */
  23.  
  24. #define ALT_BITSTREAM_READER
  25.  
  26. /* (stolen from the kernel) */
  27. #ifdef WORDS_BIGENDIAN
  28.  
  29. #    define swab32(x) (x)
  30.  
  31. #else
  32.  
  33. #    if defined (__i386__)
  34.  
  35. #    define swab32(x) __i386_swab32(x)
  36.     static inline const uint32_t __i386_swab32(uint32_t x)
  37.     {
  38.         __asm__("bswap %0" : "=r" (x) : "0" (x));
  39.         return x;
  40.     }
  41.  
  42. #    else
  43.  
  44. #    define swab32(x)\
  45. ((((uint8_t*)&x)[0] << 24) | (((uint8_t*)&x)[1] << 16) |  \
  46.  (((uint8_t*)&x)[2] << 8)  | (((uint8_t*)&x)[3]))
  47.  
  48. #    endif
  49. #endif
  50.  
  51. #ifdef ALT_BITSTREAM_READER
  52. extern uint32_t *buffer_start; 
  53. extern int indx;
  54. #else
  55. extern uint32_t a52_bits_left;
  56. extern uint32_t a52_current_word;
  57. #endif
  58.  
  59. void a52_bitstream_set_ptr (uint8_t * buf);
  60. uint32_t a52_bitstream_get_bh(uint32_t num_bits);
  61. int32_t a52_bitstream_get_bh_2(uint32_t num_bits);
  62.  
  63. static __inline uint32_t 
  64. bitstream_get(uint32_t num_bits)
  65. {
  66. #ifdef ALT_BITSTREAM_READER
  67.     uint32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
  68.  
  69.     result<<= (indx&0x07);
  70.     result>>= 32 - num_bits;
  71.     indx+= num_bits;
  72.     
  73.     return result;
  74. #else
  75.     uint32_t result;
  76.     
  77.     if(num_bits < a52_bits_left) {
  78.     result = (a52_current_word << (32 - a52_bits_left)) >> (32 - num_bits);
  79.     a52_bits_left -= num_bits;
  80.     return result;
  81.     }
  82.  
  83.     return a52_bitstream_get_bh(num_bits);
  84. #endif
  85. }
  86.  
  87. static __inline void bitstream_skip(int num_bits)
  88. {
  89. #ifdef ALT_BITSTREAM_READER
  90.     indx+= num_bits;
  91. #else
  92.     bitstream_get(num_bits);
  93. #endif
  94. }
  95.  
  96. static __inline int32_t 
  97. bitstream_get_2(uint32_t num_bits)
  98. {
  99. #ifdef ALT_BITSTREAM_READER
  100.     int32_t result= swab32( *(uint32_t *)(((uint8_t *)buffer_start)+(indx>>3)) );
  101.  
  102.     result<<= (indx&0x07);
  103.     result>>= 32 - num_bits;
  104.     indx+= num_bits;
  105.         
  106.     return result;
  107. #else
  108.     int32_t result;
  109.     
  110.     if(num_bits < a52_bits_left) {
  111.     result = (((int32_t)a52_current_word) << (32 - a52_bits_left)) >> (32 - num_bits);
  112.     a52_bits_left -= num_bits;
  113.     return result;
  114.     }
  115.  
  116.     return a52_bitstream_get_bh_2(num_bits);
  117. #endif
  118. }
  119.